package com.right.health.app.ui.me;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.bean.ServicePackResponse;
import com.right.health.app.net.UserApi;
import com.right.health.app.ui.me.adapter.ServicePackAdapter;

/**
 * @author weming
 * @Date 2021/4/16
 * @description 服务包列表
 */
public class ServicePackActivity extends BaseActivity implements OnHttpListener, View.OnClickListener {
    private static final int HTTP_CODE_GET_LIST= 1;

    private ListView mListView;
    private ServicePackAdapter mAdapter;
    private UserApi mUserApi;
    private ProgressDialog mProgressDialog;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.activity_service_pack);
    }

    @Override
    public void findView() {
        mListView = findViewById(R.id.listView);
        mAdapter = new ServicePackAdapter(this);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent=new Intent(this,ServiceDetailActivity.class);
            intent.putExtra("data",mAdapter.getItem(position));
            startActivity(intent);
        });
    }

    @Override
    public void initialize() {
        mUserApi = new UserApi();
        mUserApi.setListener(this);
        mUserApi.addRequestCode(HTTP_CODE_GET_LIST);
        mProgressDialog = ProgressDialog.show(this, "", "加载中",false,true);
        mUserApi.getServicePack();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onResponse(Request request, Response response) {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if (HTTP_CODE_GET_LIST == request.requestCode) {
                if (mProgressDialog != null) {
                    mProgressDialog.dismiss();
                }
                try {
                    ServicePackResponse listResponse=response.cast(ServicePackResponse.class);
                    mAdapter.setDataSource(listResponse.getServices());
                } catch (Exception e) {
                    e.printStackTrace();
                    ToastUtil.show(this,"数据解析失败");
                }
            }
        }
    }
}
